package cn.iocoder.yudao.module.promotion.controller.admin.rankingrecord;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.promotion.controller.admin.rankingrecord.vo.RankingRecordPageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.rankingrecord.vo.RankingRecordRespVO;
import cn.iocoder.yudao.module.promotion.controller.admin.rankingrecord.vo.RankingRecordSaveReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.rankingrecord.RankingRecordDO;
import cn.iocoder.yudao.module.promotion.service.rankingrecord.RankingRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 排行榜分佣记录")
@RestController
@RequestMapping("/promotion/ranking-record")
@Validated
public class RankingRecordController {

    @Resource
    private RankingRecordService rankingRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建排行榜分佣记录")
    @PreAuthorize("@ss.hasPermission('promotion:ranking-record:create')")
    public CommonResult<Long> createRankingRecord(@Valid @RequestBody RankingRecordSaveReqVO createReqVO) {
        return success(rankingRecordService.createRankingRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新排行榜分佣记录")
    @PreAuthorize("@ss.hasPermission('promotion:ranking-record:update')")
    public CommonResult<Boolean> updateRankingRecord(@Valid @RequestBody RankingRecordSaveReqVO updateReqVO) {
        rankingRecordService.updateRankingRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除排行榜分佣记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('promotion:ranking-record:delete')")
    public CommonResult<Boolean> deleteRankingRecord(@RequestParam("id") Long id) {
        rankingRecordService.deleteRankingRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得排行榜分佣记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('promotion:ranking-record:query')")
    public CommonResult<RankingRecordRespVO> getRankingRecord(@RequestParam("id") Long id) {
        RankingRecordDO rankingRecord = rankingRecordService.getRankingRecord(id);
        return success(BeanUtils.toBean(rankingRecord, RankingRecordRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得排行榜分佣记录")
    @Parameter(name = "id", description = "排行榜ID", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('promotion:ranking-record:query')")
    public CommonResult<List<RankingRecordRespVO>> getRankingRecordList(@RequestParam("rankingId") Long rankingId) {
        List<RankingRecordDO> rankingRecordList = rankingRecordService.getRankingRecordList(rankingId);
        if (CollectionUtils.isNotEmpty(rankingRecordList) && rankingRecordList.size() > 20) {
            rankingRecordList = rankingRecordList.subList(0, 20);
        }
        return success(BeanUtils.toBean(rankingRecordList, RankingRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得排行榜分佣记录分页")
    @PreAuthorize("@ss.hasPermission('promotion:ranking-record:query')")
    public CommonResult<PageResult<RankingRecordRespVO>> getRankingRecordPage(@Valid RankingRecordPageReqVO pageReqVO) {
        PageResult<RankingRecordDO> pageResult = rankingRecordService.getRankingRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RankingRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出排行榜分佣记录 Excel")
    @PreAuthorize("@ss.hasPermission('promotion:ranking-record:export')")
    @OperateLog(type = EXPORT)
    public void exportRankingRecordExcel(@Valid RankingRecordPageReqVO pageReqVO,
                                         HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RankingRecordDO> list = rankingRecordService.getRankingRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "排行榜分佣记录.xls", "数据", RankingRecordRespVO.class,
                BeanUtils.toBean(list, RankingRecordRespVO.class));
    }

}